package com.zy.composeworld.ui.screens.image.image

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.geometry.center
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.ColorMatrix
import androidx.compose.ui.graphics.CompositingStrategy
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.ImageShader
import androidx.compose.ui.graphics.LinearGradientShader
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.RadialGradientShader
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shader
import androidx.compose.ui.graphics.ShaderBrush
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.clipPath
import androidx.compose.ui.graphics.drawscope.inset
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.toSize
import coil.compose.AsyncImage
import com.zy.composeworld.R
import kotlin.math.roundToInt


@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun ImagePager006() {

    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
    ) {

        item {
            TextHead(text = "Brush：渐变和着色器")
            TextHead(text = "horizontalGradient")


            Canvas(
                modifier = Modifier.size(100.dp),
                onDraw = {
                    drawCircle(Brush.horizontalGradient(listOf(Color.Red, Color.Blue)))
                }
            )

            TextHead(text = "verticalGradient")
            Canvas(
                modifier = Modifier.size(100.dp),
                onDraw = {
                    drawCircle(Brush.verticalGradient(listOf(Color.Red, Color.Blue)))
                }
            )

            TextHead(text = "linearGradient")
            Canvas(
                modifier = Modifier.size(100.dp),
                onDraw = {
                    drawCircle(Brush.linearGradient(listOf(Color.Red, Color.Blue)))
                }
            )

            TextHead(text = "sweepGradient")
            Canvas(
                modifier = Modifier.size(100.dp),
                onDraw = {
                    drawCircle(Brush.sweepGradient(listOf(Color.Red, Color.Blue)))
                }
            )

            TextHead(text = "radialGradient")
            Canvas(
                modifier = Modifier.size(100.dp),
                onDraw = {
                    drawCircle(Brush.radialGradient(listOf(Color.Red, Color.Blue)))
                }
            )
        }

        item {
            TextHead(text = "使用 colorStops 更改颜色分布")
            val colorStops = arrayOf(
                0.0f to Color.Yellow,
                0.2f to Color.Red,
                1f to Color.Blue
            )
            Row {
                Box(
                    modifier = Modifier
                        .requiredSize(150.dp)
                        .background(
                            Brush.horizontalGradient(
                                listOf(
                                    Color.Yellow,
                                    Color.Red,
                                    Color.Blue
                                )
                            )
                        )
                )

                Box(
                    modifier = Modifier
                        .requiredSize(150.dp)
                        .background(Brush.horizontalGradient(colorStops = colorStops))
                )
            }

        }

        item {
            TextHead(text = "使用 TileMode 重复解锁图案")
            val listColors = listOf(Color.Yellow, Color.Red, Color.Blue)
            val tileSize = with(LocalDensity.current) {
                50.dp.toPx()
            }
            Text(text = "Repeated")
            Box(
                modifier = Modifier
                    .requiredSize(150.dp)
                    .background(
                        Brush.horizontalGradient(
                            listColors,
                            endX = tileSize,
                            tileMode = TileMode.Repeated
                        )
                    )
            )

            Text(text = "Mirror")
            Box(
                modifier = Modifier
                    .requiredSize(150.dp)
                    .background(
                        Brush.horizontalGradient(
                            listColors,
                            endX = tileSize,
                            tileMode = TileMode.Mirror
                        )
                    )
            )

            Text(text = "Clamp")
            Box(
                modifier = Modifier
                    .requiredSize(150.dp)
                    .background(
                        Brush.horizontalGradient(
                            listColors,
                            endX = tileSize,
                            tileMode = TileMode.Clamp
                        )
                    )
            )

            Text(text = "Decal")
            Box(
                modifier = Modifier
                    .requiredSize(150.dp)
                    .background(
                        Brush.horizontalGradient(
                            listColors,
                            endX = tileSize,
                            tileMode = TileMode.Decal
                        )
                    )
            )
        }

        item {
            TextHead(text = "更改 Brush 大小")
            val listColors = listOf(Color.Yellow, Color.Red, Color.Blue)
            val customBrush = remember {
                object : ShaderBrush() {
                    override fun createShader(size: Size): Shader {
                        return LinearGradientShader(
                            colors = listColors,
                            from = Offset.Zero,
                            to = Offset(size.width / 4f, 0f),
                            tileMode = TileMode.Mirror
                        )
                    }
                }
            }
            Box(
                modifier = Modifier
                    .requiredSize(200.dp)
                    .background(customBrush)
            )



            Row {
                Box(
                    modifier = Modifier
                        .requiredSize(150.dp)
                        .background(
                            Brush.radialGradient(
                                listOf(Color(0xFF2be4dc), Color(0xFF243484))
                            )
                        )
                )

                val largeRadialGradient = object : ShaderBrush() {
                    override fun createShader(size: Size): Shader {
                        val biggerDimension = maxOf(size.height, size.width)
                        return RadialGradientShader(
                            colors = listOf(Color(0xFF2be4dc), Color(0xFF243484)),
                            center = size.center,
                            radius = biggerDimension / 2f,
                            colorStops = listOf(0f, 0.95f)
                        )
                    }
                }
                Divider(modifier = Modifier.size(2.dp))

                Box(
                    modifier = Modifier
                        .requiredSize(150.dp)
                        .background(largeRadialGradient)
                )
            }

            val colorStops = arrayOf(
                0.0f to Color.Yellow,
                0.2f to Color.Red,
                1f to Color.Blue
            )
            val brush = Brush.horizontalGradient(colorStops = colorStops)
            Box(
                modifier = Modifier
                    .requiredSize(200.dp)
                    .drawBehind {
                        drawRect(brush = brush) // will allocate a shader to occupy the 200 x 200 dp drawing area
                        inset(10f) {
                            /* Will allocate a shader to occupy the 180 x 180 dp drawing area as the
                             inset scope reduces the drawing  area by 10 pixels on the left, top, right,
                            bottom sides */
                            drawRect(brush = brush)
                            inset(5f) {
                                /* will allocate a shader to occupy the 170 x 170 dp drawing area as the
                                 inset scope reduces the  drawing area by 5 pixels on the left, top,
                                 right, bottom sides */
                                drawRect(brush = brush)
                            }
                        }
                    }
            )

        }

        item {
            TextHead(text = "将图片用作笔刷")

            val imageBrush =
                ShaderBrush(ImageShader(ImageBitmap.imageResource(id = R.drawable.ic_dog1)))

// Use ImageShader Brush with background
            Box(
                modifier = Modifier
                    .requiredSize(200.dp)
                    .background(imageBrush)
            )

// Use ImageShader Brush with TextStyle
            Text(
                text = "Hello Android!",
                style = TextStyle(
                    brush = imageBrush,
                    fontWeight = FontWeight.ExtraBold,
                    fontSize = 36.sp
                )
            )

// Use ImageShader Brush with DrawScope#drawCircle()
            Canvas(onDraw = {
                drawCircle(imageBrush)
            }, modifier = Modifier.size(200.dp))
        }


    }

}


@Composable
private fun TextHead(text: String) {
    Text(text, fontWeight = FontWeight.Bold, color = Color.Red)
}